//
//  BBMainViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/9.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let path:String = Bundle.main.path(forResource: "VCProfile", ofType:"plist") ?? ""
        let data:NSMutableArray =  NSMutableArray.init(contentsOfFile: path)!
        for dic in data as![[String:String]]  {
            self.addChild(childControllerName: dic["vcName"]!, title: dic["title"]!, image: dic["imageName"]!)
        }
    }
    
    /// 根据名称初始化子控制器并添加到tabbar
    ///
    /// - Parameters:
    ///   - childControllerName: 控制器名称
    ///   - title: 控制器标题
    ///   - image: 控制器tabbar图片
    private func addChild(childControllerName:String, title:String, image:String){
        guard let namespace = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String else{
            NSLog("获取命名空间失败")
            return
        }
        let cls:AnyClass?=NSClassFromString(namespace+"."+childControllerName)
        guard let typeClass = cls as?UIViewController.Type else{
            NSLog( "cls 不能当做UIViewController")
            return
        }
        let childController = typeClass.init()
        childController.tabBarItem.image = UIImage.init(named: image)
        childController.tabBarItem.selectedImage = UIImage.init(named: image+"_highlighted")
        childController.title = title
        //包装一个导航控制器
        let nav = UINavigationController()
        nav.addChild(childController)
        //将导航控制器添加到当前控制器
        self.addChild(nav)
    }
}

